package cn.yunyichina.provider.pay.wechat.service;

import cn.yunyichina.provider.pay.wechat.entity.WechatAppPayResponse;
import cn.yunyichina.provider.pay.wechat.entity.WechatJsapiPayResponse;
import cn.yunyichina.provider.pay.wechat.entity.WechatMedicalPayResponse;
import cn.yunyichina.provider.pay.wechat.entity.WechatNativePayResponse;
import cn.yunyichina.utils.commons.RandomUtil;
import cn.yunyichina.utils.security.SignUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Lullaby on 2016/10/14.
 */
public class BasePayService extends BaseService {

    /**
     * 构建微信公众号支付返回客户端出参
     *
     * @param response
     * @param apiSecret
     * @return
     */
    protected Map<String, String> buildWechatJsapiPayResponse(WechatJsapiPayResponse response, String apiSecret) {
        Map<String, String> result = new HashMap<>();

        result.put("appId", response.getAppid());
        result.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000));
        result.put("nonceStr", RandomUtil.getRandomALLChar(32));
        result.put("package", "prepay_id=" + response.getPrepayId());
        result.put("signType", "MD5");
        result.put("paySign", SignUtil.wechatPaySign(result, apiSecret));

        return result;
    }

    /**
     * 构建微信APP支付返回客户端出参
     *
     * @param response
     * @param apiSecret
     * @return
     */
    protected Map<String, String> buildWechatAppPayResponse(WechatAppPayResponse response, String apiSecret) {
        Map<String, String> result = new HashMap<>();

        result.put("appid", response.getAppid());
        result.put("partnerid", response.getMchId());
        result.put("prepayid", response.getPrepayId());
        result.put("package", "Sign=WXPay");
        result.put("noncestr", RandomUtil.getRandomALLChar(32));
        result.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
        result.put("sign", SignUtil.wechatPaySign(result, apiSecret));

        return result;
    }

    /**
     * 构建微信扫码付返回客户端出参
     *
     * @param response
     * @return
     */
    protected Map<String, String> buildWechatNativePayResponse(WechatNativePayResponse response) {
        Map<String, String> result = new HashMap<>();

        result.put("trade_type", response.getTradeType());
        result.put("prepay_id", response.getPrepayId());
        result.put("code_url", response.getCodeUrl());

        return result;
    }

    /**
     * 构建微信医疗支付返回客户端出参
     *
     * @param response
     * @return
     */
    protected Map<String, String> buildWechatMedicalPayResponse(WechatMedicalPayResponse response) {
        Map<String, String> result = new HashMap<>();

        result.put("med_trans_id", response.getMedTransId());
        result.put("pay_url", response.getPayUrl());

        return result;
    }

}
